
#include "key.h"

void key_init(key_t *t, uint16_t interval, uint8_t valid_level)
{
    t->valid_level = valid_level;
    debouncing_signal_init(&t->s, interval, valid_level);
}

uint8_t key_scan(key_t *key, uint16_t tick)
{
    uint8_t result = 0;
    uint8_t level = key->bus->read(key->bus_handle);
    uint8_t state_new = debouncing_signal_in(&key->s, tick, level)^key->valid_level;
    if(key->state!=state_new) {
        key->change = 1;
        result = 1;
    }
    key->state = state_new;
    return result;
}
